#pragma once
#include"ThreadCache.h"
#include"PageCache.h"

static void* concurrentAlloc(size_t size)
{
	if (size > byte_MAX)
	{
		size_t alignsize = sizeclass::Rundup(size);
		size_t kpage = alignsize >> Page_shift;
		PageCache::GetInit()->_pagemtx.lock();
		Span* span = PageCache::GetInit()->newspan(kpage);
		PageCache::GetInit()->_pagemtx.unlock();

		void* ptr = (void*)(span->_pageid << Page_shift);
		return ptr;
	}
	if (pthreadpool == nullptr)
	{
		static ObjectPool< ThreadCache> trheadpool;
		pthreadpool =  trheadpool.New();
	}
	return pthreadpool->Allcoate(size);
}


static void concurrentfree(void* ptr, size_t size)
{
	if (size > byte_MAX)
	{
		Span* span = PageCache::GetInit()->GetObjspan(ptr);

		PageCache::GetInit()->_pagemtx.lock();

		PageCache::GetInit()->RealsespanTopagecache(span);
		PageCache::GetInit()->_pagemtx.unlock();

	}
	else
	{
		assert(pthreadpool);
		return pthreadpool->DeAllocate(size, ptr);
	}
	
 }